﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entity;
using Business.Manager;
using Business.IManager;

namespace NetWorkPayment.BcakWard
{
    public partial class PoatPaymentProject : System.Web.UI.Page
    {
        DataTable dataTable = new DataTable();
        DataRow dataRow;
        DataRow[] filterByIDRows;
        IPaymentProjectManager paymentProjectManager = new PaymentProjectManager();
        IPaymentItemManager paymentItemManager = new PaymentItemManager();
        IAcademyManager academyManager = new AcademyManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                GenerateDataTable();
                paymentList.DataSource = dataTable;
                paymentList.DataBind();
                Session["dt"] = dataTable;
                if (Session["Sum"]!=null)
                {
                  txtSum.Value = Convert.ToString(Session["Sum"]);
                }
               BindProjectType();
               BindAccadamy();
               BindObjectYear();
               BindMajor();

            }
        }
         
        private void GenerateDataTable()
        {
            DataColumn dataColumn;
            dataColumn = new DataColumn("ID", Type.GetType("System.Int32"));
            dataColumn.AutoIncrement = true;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn("txtProName");
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn("txtAmount");
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn("ckSerprate");
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn("btnDelete");
            dataTable.Columns.Add(dataColumn);
            dataRow = dataTable.NewRow();
            dataTable.Rows.Add(dataRow);
        }

       
        protected void Refresh()
        {
            paymentList.DataSource = dataTable;
            paymentList.DataBind();
           
        }
        protected void Save()
        {
            int id;
            dataTable = Session["dt"] as DataTable;
            foreach (GridViewRow gridViewRow in paymentList.Rows)
            {
                id = int.Parse((gridViewRow.FindControl("lblID") as Label).Text);
                filterByIDRows = dataTable.Select("ID = " + id);
                filterByIDRows[0]["txtProName"] = (gridViewRow.FindControl("txtProName") as TextBox).Text;
                filterByIDRows[0]["txtAmount"] = (gridViewRow.FindControl("txtAmount") as TextBox).Text;
                filterByIDRows[0]["ckSerprate"] = (gridViewRow.FindControl("ckSerprate") as CheckBox).Checked;
            }
            Session["Sum"] = Convert.ToInt32(txtSum.Value.ToString());
            String s = Convert.ToString( Session["Sum"]);
        }

        protected void paymentList_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id;
            id = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "DeleteData":
                    Delete(id);
                    Refresh();
                    break;
            }
        }

        protected void Delete(int id)
        {
            dataTable = Session["dt"] as DataTable;
            filterByIDRows = dataTable.Select("ID = " + id);
            dataTable.Rows.Remove(filterByIDRows[0]);
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {

            Save();
            dataRow = dataTable.NewRow();
            dataTable.Rows.Add(dataRow);
            Refresh();

        }

        /// <summary>
        /// 添加缴费项
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            PaymentProject project = new PaymentProject();
            project.PaymentProjectId = paymentProjectManager.getMaxProjectId();
            project.PaymentBeginTime = Convert.ToDateTime(txtBeginTime.Text);
            project.PaymentEndTime = Convert.ToDateTime(txtEndTime.Text);
            project.OperatorId = "1";//要改


            project.Target = objectMajor.SelectedValue+objectYear.SelectedValue;


            project.ProjectTypeId = Convert.ToInt32(projectType.SelectedValue);
            project.ProjectName = txtProjectName.Text;//可删掉
            project.TotalAmount = Convert.ToDecimal(txtSum.Value);
            project.Year = Convert.ToDateTime(txtBeginTime.Text).Year;//System.DateTime.Now.Year;
            project.ProjectDetailList=new List<PaymentProjectDetail>();
            decimal sum = 0;
            //
            int detailBeginId = paymentItemManager.getMaxProjectDetailId();
            foreach (GridViewRow row in this.paymentList.Rows)
            {
                PaymentProjectDetail detail=new PaymentProjectDetail();
                detail.PayProjectDetailId = detailBeginId;
                detail.ProDetailName=(row.FindControl("txtProName") as TextBox).Text;
                detail.ProDetailAmount = Convert.ToDecimal((row.FindControl("txtAmount") as TextBox).Text);
                
                sum += detail.ProDetailAmount;
                Control ctrl = row.FindControl("ckSerprate");
                if ((ctrl as CheckBox).Checked)
                {
                    detail.SeperatePay = true;
                }
                else
                {
                    detail.SeperatePay = false;
                }
                detail.PaymentProjectId = project.PaymentProjectId;
                project.ProjectDetailList.Add(detail);
                detailBeginId++;
            }
            project.TotalAmount = sum;
            paymentProjectManager.generateProject(project);
            
        }
        public void BindObjectYear()
        {
            int month = System.DateTime.Now.Month;
            String[] years = new String[4];
            if (month < 7)
            {
                int year = System.DateTime.Now.Year - 5;
                for (int i = 0; i < 4; i++)
                {
                    year++;
                    string s = Convert.ToString(year);
                    years[i] = s;
                }
            }
            else
            {
                int year = System.DateTime.Now.Year - 4;
                for (int i = 0; i < 4; i++)
                {
                    year++;
                    string s = Convert.ToString(year);
                    years[i] = s;
                }
            }
            objectYear.DataSource =years;
            objectYear.DataBind();
        }
        public void BindAccadamy()
        {
            objectAcadamy.DataSource = academyManager.getObejctAccadamy().Tables[0].DefaultView;
            objectAcadamy.DataTextField = "acaName";
            objectAcadamy.DataValueField = "acaId";
            objectAcadamy.DataBind();
            objectAcadamy.AutoPostBack = true;
        }
        public void BindProjectType()
        {
            projectType.DataSource = paymentProjectManager.getPaymentType().Tables[0].DefaultView;
            projectType.DataTextField = "typeName";
            projectType.DataValueField = "tid";
            projectType.DataBind();
            projectType.AutoPostBack = true;
        }
        public void BindMajor()
        {
            objectMajor.DataSource = academyManager.getObjectMajor().Tables[0].DefaultView;
            objectMajor.DataTextField = "majName";
            objectMajor.DataValueField = "majId";
            objectMajor.DataBind();
            objectMajor.AutoPostBack = true;
        }
        public void BindMajor(String acadamyId)
        {

            objectMajor.DataSource = academyManager.getObjectMajor(acadamyId);
            objectMajor.DataTextField = "majName";
            objectMajor.DataValueField = "majId";
            objectMajor.DataBind();
            objectMajor.AutoPostBack = true;
        }

        protected void objectAcadamy_SelectedIndexChanged(object sender, EventArgs e)
        {
            String majorId = objectAcadamy.SelectedItem.Value;
            BindMajor(majorId);
        }

        protected void btn_addPaymentProject_Click(object sender, EventArgs e)
        {

        }
    }
}